
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author dttung
 */
@WebServlet("/ShowCup")
public class ShowCup extends HttpServlet
{
    private int nCups = 5; // 4 cups and 1 final Allstars playoff
    private Utils myUtil = new Utils();
    public final String systemdir = "C:\\Users\\dttung\\Documents\\NetBeansProjects\\2013-2014\\src\\java\\";
    //public final String systemdir = "/var/lib/openshift/5224c8d94382ec6f2b00034b/jbossews/yff/";
    //public final String systemdir = "/Users/dttung/NetBeansProjects/trunk/2013-2014/src/java/";
    private HashMap<String, Integer> cupPoints = new HashMap<String, Integer>();
    private HashMap<String, Float> cupDiff = new HashMap<String, Float>();
    private HashMap<String, String> firstTeamsFor16 = new HashMap<String, String>(); // danh sach doi dung dau cac bang
    private HashMap<String, String> secondTeamsFor16 = new HashMap<String, String>(); // danh sach doi dung nhi cac bang
    
    private ArrayList<String> firstTeamsFor8 = new ArrayList<String>();
    private ArrayList<String> secondTeamsFor8 = new ArrayList<String>();
    
    private ArrayList<String> firstTeamsFor4 = new ArrayList<String>();
    private ArrayList<String> secondTeamsFor4 = new ArrayList<String>();
    
    private String finalTeam1, finalTeam2; // hai doi da chung ket
    
    private HashMap teamscores = new HashMap();
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, FileNotFoundException
    {
        try {
            myUtil.readYffScores();
            myUtil.readTeamScores();
        } catch (URISyntaxException ex) {
            Logger.getLogger(ShowCup.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        ServletUtil.printFistHalf(out);
        
        String selcup = request.getParameter("selcup");
        if (selcup == null) selcup = "1";
        printForm(out,Integer.valueOf(selcup));
        // init points
        initCupPointsAndDiff(selcup);
        // calculate points
        calculateCupPoints(selcup);
        // print tables
        printStyles(out);
        
        // in kết quả các trận vòng bảng
        printRound(1, selcup, "tftableb", out);        
        printRound(2, selcup, "tftableg", out);
        printRound(3, selcup, "tftableb", out);
        
        // in 8 bảng
        printTables(out, selcup);
        // print table for Vòng loại trực tiếp 
        // Neu la vong 1-16 thi in ra
        int currweek = myUtil.getCurrentRound();
        int round = currweek - 2 - (Integer.valueOf(selcup) - 1)*8;
        if (round >= 4) // round = 4 nghia la den vong 1/16
            print16(out, selcup, "tftableb");
        if (round >=5) // vong 1/8
            print8(out, selcup, "tftableg");
        if (round >=6) // vong 1/8
            print4(out, selcup, "tftableb");
        ServletUtil.printSecondHalf(out);
        
    }
    private void initCupPointsAndDiff(String selcup) throws UnsupportedEncodingException, FileNotFoundException, IOException
    { 
        initCupPointsForTableinCup("A", selcup);
        initCupPointsForTableinCup("B", selcup);
        initCupPointsForTableinCup("C", selcup);
        initCupPointsForTableinCup("D", selcup);
        initCupPointsForTableinCup("E", selcup);
        initCupPointsForTableinCup("F", selcup);
        initCupPointsForTableinCup("G", selcup);
        initCupPointsForTableinCup("H", selcup);
        
        initCupDiffForTableinCup("A", selcup);
        initCupDiffForTableinCup("B", selcup);
        initCupDiffForTableinCup("C", selcup);
        initCupDiffForTableinCup("D", selcup);
        initCupDiffForTableinCup("E", selcup);
        initCupDiffForTableinCup("F", selcup);
        initCupDiffForTableinCup("G", selcup);
        initCupDiffForTableinCup("H", selcup);
    }
    private void initCupPointsForTableinCup(String table, String selcup) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        ArrayList<String> teams = getMatches(table, selcup, 1);
        for (int i = 0; i < teams.size(); i++)
        {
            cupPoints.put(teams.get(i), 0);
        }
    }
    private void initCupDiffForTableinCup(String table, String selcup) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        ArrayList<String> teams = getMatches(table, selcup, 1);
        for (int i = 0; i < teams.size(); i++)
        {
            cupDiff.put(teams.get(i), new Float(0));
        }
    }
    private void calculateCupPoints(String selcup) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        // lay tuan hien tai
        int currweek = myUtil.getCurrentRound();
        int cup = Integer.valueOf(selcup);
        int cupround = currweek - 2 - (cup -1)*8; // xem lai cach tinh week theo cup va round
        if (cupround > 3) cupround = 3;
        for (int i = 1; i <= cupround; i++) // i = round 1, 2, up to 3
        {
            // Bang A
            calculateCupPointAtRoundForTable(i, "A", selcup);
            // Bang B
            calculateCupPointAtRoundForTable(i, "B", selcup);
            // Bang C
            calculateCupPointAtRoundForTable(i, "C", selcup);
            // Bang D
            calculateCupPointAtRoundForTable(i, "D", selcup);
            // Bang E
            calculateCupPointAtRoundForTable(i, "E", selcup);
            // Bang F
            calculateCupPointAtRoundForTable(i, "F", selcup);
            // Bang G
            calculateCupPointAtRoundForTable(i, "G", selcup);
            // Bang H
            calculateCupPointAtRoundForTable(i, "H", selcup);
            
        }
    }
    private void calculateCupPointAtRoundForTable(int round, String table, String selcup) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        // Lay cac tran dau
        int cup = Integer.valueOf(selcup);
        ArrayList matches = getMatches(table, selcup, round);
        String team1 = (String)matches.get(0);
        String team2 = (String)matches.get(1);
        int week = (cup - 1) * 8 + 2 + round;
        float score1 = Float.valueOf(myUtil.getWeekYffPoint(team1, week));
        float score2 = Float.valueOf(myUtil.getWeekYffPoint(team2, week));
        float diff = score1 - score2;
        
        cupDiff.put(team1, cupDiff.get(team1) + diff);
        cupDiff.put(team2, cupDiff.get(team2) - diff);
        
        if (diff >= 5) // team 1 win
        {
            cupPoints.put(team1, cupPoints.get(team1) + 3);
        } else if (diff <= -5) // team 2 win
        {
            cupPoints.put(team2, cupPoints.get(team2) + 3);
        } else // hai doi hoa
        {
            cupPoints.put(team1, cupPoints.get(team1) + 1);
            cupPoints.put(team2, cupPoints.get(team2) + 1);
        }
        
        String team3 = (String)matches.get(2);
        String team4 = (String)matches.get(3);


        //int week = (cup - 1) * 8 + 2 + round;
        float score3 = Float.valueOf(myUtil.getWeekYffPoint(team3, week));
        float score4 = Float.valueOf(myUtil.getWeekYffPoint(team4, week));
        diff = score3 - score4;
        
        cupDiff.put(team3, cupDiff.get(team3) + diff);
        cupDiff.put(team4, cupDiff.get(team4) - diff);
        
        if (diff >= 5) // team 3 win
        {
            cupPoints.put(team3, cupPoints.get(team3) + 3);
        } else if (diff <= -5) // team 4 win
        {
            cupPoints.put(team4, cupPoints.get(team4) + 3);
        } else // hai doi hoa
        {
            cupPoints.put(team3, cupPoints.get(team3) + 1);
            cupPoints.put(team4, cupPoints.get(team4) + 1);
        }
    }
    public void print16match(PrintWriter out, String tbl1, String tbl2, boolean odd, int weekOf16)
    {
        // Nhat A gap nhi B
        String team1 = firstTeamsFor16.get(tbl1);
        String team2 = secondTeamsFor16.get(tbl2);
        float point1 = Float.valueOf(myUtil.getWeekYffPoint(team1, weekOf16));
        float point2 = Float.valueOf(myUtil.getWeekYffPoint(team2, weekOf16));
        boolean firstwin;
        if (point1 > point2) firstwin = true;
        else if (point2 > point1) firstwin = false;
        else 
        {
            // so sanh tong diem yff
            float totalyff1 = myUtil.getTotalYffPoint(team1);
            float totalyff2 = myUtil.getTotalYffPoint(team2);
            if (totalyff1 > totalyff2) firstwin = true;
            else firstwin = false;
        }
        if (firstwin)
        {
            if (odd) firstTeamsFor8.add(team1);
            else secondTeamsFor8.add(team1);
            out.println("<tr><td><strong>" + team1 + "</strong></td><td>" + team2
                               + "</td><td><strong>" + point1 + "</strong>&nbsp-&nbsp" + point2 + "</td></tr>");
        } else 
        {
            if (odd) firstTeamsFor8.add(team2);
            else secondTeamsFor8.add(team2);
            out.println("<tr><td>" + team1 + "</td><td><strong>" + team2
                               + "</strong></td><td>" + point1 + "&nbsp-&nbsp<strong>" + point2 + "</strong></td></tr>");
        }
    }
    public void print16(PrintWriter out, String selcup, String tbstyle)
    {
        // get yff week based on selcup
        int cup = Integer.valueOf(selcup);
        int weekOf16 = (cup - 1) * 8 + 2 + 4; // round 4 = 16
        out.println("<div align=\"center\" id=\"tbl16\"><table border=\"0\" width=\"80%\"><tr><td>");
        out.println("<table  class=\""+tbstyle+"\" border=\"1\">\n" +
                    "<caption><strong>Kết quả vòng 1/16 <br/></strong></caption>" +
                    "<tr><th>Đội 1</th><th>Đội 2</th><th>Kết quả</th></tr>\n");
        
        print16match(out, "A", "B", true, weekOf16);
        print16match(out, "C", "D", false, weekOf16);
        print16match(out, "E", "F", true, weekOf16);
        print16match(out, "G", "H", false, weekOf16);
        print16match(out, "B", "A", true, weekOf16);
        print16match(out, "D", "C", false, weekOf16);
        print16match(out, "F", "E", true, weekOf16);
        print16match(out, "H", "G", false, weekOf16);
        
        
        out.println(                    "</table>");
        out.println("</td></tr></table></div>");
    }
    
    public void print8match(PrintWriter out, int match, boolean odd, int weekOf8)
    {
        String team1 = firstTeamsFor8.get(match);
        String team2 = secondTeamsFor8.get(match);
        float point1 = Float.valueOf(myUtil.getWeekYffPoint(team1, weekOf8));
        float point2 = Float.valueOf(myUtil.getWeekYffPoint(team2, weekOf8));
        boolean firstwin;
        if (point1 > point2) firstwin = true;
        else if (point2 > point1) firstwin = false;
        else 
        {
            // so sanh tong diem yff
            float totalyff1 = myUtil.getTotalYffPoint(team1);
            float totalyff2 = myUtil.getTotalYffPoint(team2);
            if (totalyff1 > totalyff2) firstwin = true;
            else firstwin = false;
        }
        
        if (firstwin)
        {
            if (odd) firstTeamsFor4.add(team1);
            else secondTeamsFor4.add(team1);
            out.println("<tr><td><strong>" + team1 + "</strong></td><td>" + team2
                               + "</td><td><strong>" + point1 + "</strong>&nbsp-&nbsp" + point2 + "</td></tr>");
        } else 
        {
            if (odd) firstTeamsFor4.add(team2);
            else secondTeamsFor4.add(team2);
            out.println("<tr><td>" + team1 + "</td><td><strong>" + team2
                               + "</strong></td><td>" + point1 + "&nbsp-&nbsp<strong>" + point2 + "</strong></td></tr>");
        }
        
    }
    public void print8(PrintWriter out, String selcup, String tbstyle)
    {
        // get yff week based on selcup
        int cup = Integer.valueOf(selcup);
        int weekOf8 = (cup - 1) * 8 + 2 + 5; // round 5 = tu ket 8 doi
        out.println("<div align=\"center\" id=\"tbl8\"><table border=\"0\" width=\"80%\"><tr><td>");
        out.println("<table class=\""+tbstyle+"\" border=\"1\">\n" +
                    "<caption><strong>Kết quả vòng 1/8 <br/></strong></caption>" +
                    "<tr><th>Đội 1</th><th>Đội 2</th><th>Kết quả</th></tr>\n");
        
        print8match(out, 0, true, weekOf8);
        print8match(out, 1, false, weekOf8);
        print8match(out, 2, true, weekOf8);
        print8match(out, 3, false, weekOf8);
        
        out.println(                    "</table>");
        out.println("</td></tr></table></div>");
    }
    public void print4match(PrintWriter out, int match, boolean odd, int weekOf4)
    {
        String team1 = firstTeamsFor4.get(match);
        String team2 = secondTeamsFor4.get(match);
        float point1 = Float.valueOf(myUtil.getWeekYffPoint(team1, weekOf4));
        float point2 = Float.valueOf(myUtil.getWeekYffPoint(team2, weekOf4));
        boolean firstwin;
        if (point1 > point2) firstwin = true;
        else if (point2 > point1) firstwin = false;
        else 
        {
            // so sanh tong diem yff
            float totalyff1 = myUtil.getTotalYffPoint(team1);
            float totalyff2 = myUtil.getTotalYffPoint(team2);
            if (totalyff1 > totalyff2) firstwin = true;
            else firstwin = false;
        }
        
        if (firstwin)
        {
            if (odd) finalTeam1 = team1;
            else finalTeam2 = team1;
            out.println("<tr><td><strong>" + team1 + "</strong></td><td>" + team2
                               + "</td><td><strong>" + point1 + "</strong>&nbsp-&nbsp" + point2 + "</td></tr>");
        } else 
        {
            if (odd) finalTeam1 = team2;
            else finalTeam2 = team2;
            out.println("<tr><td>" + team1 + "</td><td><strong>" + team2
                               + "</strong></td><td>" + point1 + "&nbsp-&nbsp<strong>" + point2 + "</strong></td></tr>");
        }
    }
    public void print4(PrintWriter out, String selcup, String tbstyle)
    {
        // get yff week based on selcup
        int cup = Integer.valueOf(selcup);
        int weekOf4 = (cup - 1) * 8 + 2 + 6; // round 6 = ban ket 4 doi
        out.println("<div align=\"center\" id=\"tbl4\"><table border=\"0\" width=\"80%\" bordercolor=\"\" style=\"background-color:\" cellpadding=\"3\" cellspacing=\"3\"><tr><td>");
        out.println("<table class=\""+tbstyle+"\" border=\"1\" width=\"100%\">\n" +
                    "<caption><strong>Kết quả vòng 1/4 <br/></strong></caption>" +
                    "<tr><th>Đội 1</th><th>Đội 2</th><th>Kết quả</th></tr>\n");
        
        print4match(out, 0, true, weekOf4);
        print4match(out, 1, false, weekOf4);
        
        out.println(                    "</table>");
        out.println("</td></tr></table></div>");
    }
    
    public void printStyles(PrintWriter out)
    {
        out.println("<style type=\"text/css\">\n" +
                    "table.tftableb {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}\n" +
                    "table.tftableb th {font-size:12px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}\n" +
                    "table.tftableb tr {background-color:#d4e3e5;}\n" +
                    "table.tftableb td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}\n" +
                    "</style>\n");
        out.println("<style type=\"text/css\">\n" +
                    "table.tftableg {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #9dcc7a;border-collapse: collapse;}\n" +
                    "table.tftableg th {font-size:12px;background-color:#abd28e;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;text-align:left;}\n" +
                    "table.tftableg tr {background-color:#bedda7;}\n" +
                    "table.tftableg td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;}\n" +
                    "</style>");
        out.println("<script type=\"text/javascript\">" +                
                "function showPageElement(what, checkboxId)\n" +
                "{\n" +
                "    var obj = typeof what == 'object'\n" +
                "        ? what : document.getElementById(what);\n" +
                "\n" +
                "    obj.style.display = 'block';\n" +
                "    return false;\n" +
                "}\n" +
                "\n" +
                "function hidePageElement(what)\n" +
                "{\n" +
                "    var obj = typeof what == 'object'\n" +
                "        ? what : document.getElementById(what);\n" +
                "\n" +
                "    obj.style.display = 'none';\n" +
                "    return false;\n" +
                "}\n" +
                "\n" +
                "function togglePageElementVisibility(what,checkboxId)\n" +
                "{\n" +                
                "    var obj = typeof what == 'object'\n" +
                "        ? what : document.getElementById(what);\n" +
                "\n" +
                "    if (obj.style.display == 'none')\n" +
                "        obj.style.display = 'block';\n" +
                "    else\n" +
                "        obj.style.display = 'none';\n" +
                "    cb = document.getElementById(checkboxId);"
                + "  if (cb.checked == TRUE) cb.checked = FALSE;"
                + "  else cb.checked = TRUE;\n" +
                "    return false;\n" +                
                "}\n" +
                "</script>");
    }
    
    public void printTables(PrintWriter out, String selcup) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        
        out.println("<table id=\"tbltables\" border=\"0\" bordercolor=\"\" style=\"background-color:\" width=\"100%\" cellpadding=\"3\" cellspacing=\"3\">\n" +
                    "	<tr>\n" +
                    "		<td width=\"50%\">");
        printTable("A", selcup, "tftableb", out);
        out.println("</td>\n" +
                    "		<td width=\"50%\">");
        printTable("B", selcup, "tftableg", out);
        
        out.println("</td>\n" +
                    "	</tr>\n" +
                    "	<tr>\n" +
                    "		<td width=\"50%\">");
        printTable("C", selcup, "tftableg", out);
        out.println(        "</td>\n" +
                    "		<td width=\"50%\">");
        printTable("D", selcup, "tftableb", out);
        out.println("        </td>\n" +
                    "	</tr>\n" +
                    "	<tr>\n" +
                    "		<td width=\"50%\">");
        printTable("E", selcup, "tftableb", out);
        out.println("           </td>\n" +
                    "		<td width=\"50%\">");
        printTable("F", selcup, "tftableg", out);
        out.println("           </td>\n" +
                    "	</tr>\n" +
                    "	<tr>\n" +
                    "		<td width=\"50%\">");
        printTable("G", selcup, "tftableg", out);
        out.println("           </td>\n" +
                    "		<td width=\"50%\">");
        printTable("H", selcup, "tftableb", out);
        out.println("           </td>\n" +
                    "	</tr>\n" +
                    "</table>");
    }
    private TreeMap getSortedTeamsForTableInCup(String table, String selcup, int currWeek) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        HashMap<String, Integer> teamsTbl = new HashMap<String, Integer>();
        ArrayList<String> teams = getMatches(table, selcup, 1);
        for (int i = 0; i < teams.size(); i++)
        {
            teamsTbl.put(teams.get(i), cupPoints.get(teams.get(i)));
        }
        CupScoreComparator comp = new CupScoreComparator(cupPoints, cupDiff, currWeek, table, selcup);
        TreeMap sortedTeamsInTable = new TreeMap(comp);
        sortedTeamsInTable.putAll(teamsTbl);
        
        Iterator it = sortedTeamsInTable.entrySet().iterator();
        Map.Entry pairs = (Map.Entry)it.next();
        firstTeamsFor16.put(table, (String) pairs.getKey());
        pairs = (Map.Entry)it.next();
        secondTeamsFor16.put(table, (String) pairs.getKey());
        return sortedTeamsInTable;
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
    public void printForm(PrintWriter out, int cupselected)
    {
        out.println("<form action=\"ShowCup\" method=\"POST\">\n" +
"                <strong>Lịch đấu / Kết quả &nbsp</strong> 	\n" +
"                <select name=\"selcup\" onchange=\"this.form.submit()\">\n");
        for (int i = 0; i < nCups - 1; i++)
        {
            int value = i + 1;
            if (value == cupselected)
                out.println("                                    <option value=\"" + value + "\" selected>Cúp " + value + "</option>\n");
            else
                out.println("                                    <option value=\"" + value + "\">Cúp " + value + "</option>\n");
        }
        if  (cupselected == 5) // all star
        {
            out.println("                                    <option value=\"" + 5 + "\" selected>All Stars Playoff</option>\n");
        }
        else
        {
            out.println("                                    <option value=\"" + 5 + "\">All Stars Playoff</option>\n");
        }
        out.println(
"                </select>\n" +
"                <input type=\"checkbox\" id=\"cbluot1\" checked " +
"                   onclick=\"togglePageElementVisibility('tblround1', 'cbluot1')\">Lượt 1"    +
" &nbsp"                +
"                <input type=\"checkbox\" id=\"cbluot2\" checked " +
"                   onclick=\"togglePageElementVisibility('tblround2', 'cbluot2')\">Lượt 2"    +
" &nbsp"                +                
" &nbsp"                +
"                <input type=\"checkbox\" id=\"cbluot3\" checked " +
"                   onclick=\"togglePageElementVisibility('tblround3', 'cbluot3')\">Lượt 3"    +
" &nbsp"                +                 
"                <input type=\"checkbox\" id=\"cbtables\" checked " +
"                   onclick=\"togglePageElementVisibility('tbltables','cbtables')\">Bảng đấu" +
"                <input type=\"checkbox\" id=\"cb16\" checked " +
"                   onclick=\"togglePageElementVisibility('tbl16','cb16')\">1/16" +
"                <input type=\"checkbox\" id=\"cb8\" checked " +
"                   onclick=\"togglePageElementVisibility('tbl8','cb8')\">1/8" +
"                <input type=\"checkbox\" id=\"cb4\" checked " +
"                   onclick=\"togglePageElementVisibility('tbl4','cb4')\">1/4" +      
"                <input type=\"checkbox\" id=\"cbck\" checked " +
"                   onclick=\"togglePageElementVisibility('tblck','cbck')\">Chung kết" +
"                <br />\n" +                
"            </form>");
    }
    public void printTable(String tbname, String cup, String tbstyle, PrintWriter out) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        out.println("<table id=\"tfhover\" class=\""+tbstyle+"\" border=\"1\">\n" +
                    "<caption>Bảng&nbsp" + tbname +"<br></caption>" +
                    "<tr><th>Thứ tự</th><th>Đội</th><th>Điểm</th><th>Hiệu số</th></tr>\n");
        
        TreeMap sortedTeams = getSortedTeamsForTableInCup(tbname, cup, myUtil.getCurrentRound());
        Iterator it = sortedTeams.entrySet().iterator();
        int i = 0;
        while (it.hasNext())
        {
            i++;
            Map.Entry pairs = (Map.Entry)it.next();
            String team = (String) pairs.getKey();
            int point = (Integer) pairs.getValue();
            out.println("<tr><td>" + i + "</td><td>" + team + "</td><td>" + point + "</td><td>" + cupDiff.get(team) + "</td></tr>\n");
        }
        //ArrayList<String> teams = getMatches(tbname, cup, 1);
        
        out.println(
                    //"<tr><td>1</td><td>" + teams.get(0) + "</td><td>" + cupPoints.get(teams.get(0)) + "</td><td>N/A</td></tr>\n" +
                    //"<tr><td>2</td><td>" + teams.get(1) + "</td><td>" + cupPoints.get(teams.get(1)) + "</td><td>N/A</td></tr>\n" +
                    //"<tr><td>3</td><td>" + teams.get(2) + "</td><td>" + cupPoints.get(teams.get(2)) + "</td><td>N/A</td></tr>\n" +
                    //"<tr><td>4</td><td>" + teams.get(3) + "</td><td>" + cupPoints.get(teams.get(3)) + "</td><td>N/A</td></tr>\n" +
                    "</table>");
    }
    public void printRound(int round, String cup, String tbstyle, PrintWriter out) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        int iCup = Integer.valueOf(cup);
        int week = (iCup - 1) * 8 + 2 + round; // 1 cup het 7 vong + 1 vong nghi, bat dau cup tu week 3
        out.println("<div align=\"center\" id=\"tblround" + round +"\"><table border=\"0\" width=\"80%\"><tr><td>");
        out.println("<table class=\""+tbstyle+"\" border=\"1\">\n" +
                    "<caption><strong>Kết quả vòng &nbsp" + round +"<br/></strong></caption>" +
                    "<tr><th>Bảng</th><th>Đội 1</th><th>Đội 2</th><th>Kết quả</th></tr>\n");
        // Table A
        ArrayList<String> matches = getMatches("A", cup, round);
        
        out.println("<tr><td>A</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>" + myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>A</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>" + myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
//                    "</table>");
        // Table B
        matches = getMatches("B", cup, round);
        
        out.println("<tr><td>B</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>B</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        // Table C
        matches = getMatches("C", cup, round);
        
        out.println("<tr><td>C</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>C</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        
        // Table D
        matches = getMatches("D", cup, round);
        
        out.println("<tr><td>D</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>D</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        // Table E
        matches = getMatches("E", cup, round);
        
        out.println("<tr><td>E</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>E</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        
        // Table F
        matches = getMatches("F", cup, round);
        
        out.println("<tr><td>F</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>F</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        
        // Table G
        matches = getMatches("G", cup, round);
        
        out.println("<tr><td>G</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>G</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        
        // Table H
        matches = getMatches("H", cup, round);
        
        out.println("<tr><td>H</td><td>" + matches.get(0) + "</td><td>" + matches.get(1) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(0), week) + "-" + myUtil.getWeekYffPoint(matches.get(1), week) + "</td></tr>\n" +
                    "<tr><td>H</td><td>" + matches.get(2) + "</td><td>" + matches.get(3) + "</td><td>"+ myUtil.getWeekYffPoint(matches.get(2), week) + "-" + myUtil.getWeekYffPoint(matches.get(3), week) + "</td></tr>\n");
        
        out.println(                    "</table>");
        out.println("</td></tr></table></div>");
    }
    private ArrayList<String> getMatches(String tbname, String cup, int round) throws UnsupportedEncodingException, FileNotFoundException, IOException
    {
        File tablefile = new File(systemdir + "tab_" + tbname + "_" + cup + ".txt" );
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(tablefile),"UTF-8"));
        String line = "";
        ArrayList<String> teams = new ArrayList<String>();
        ArrayList<String> matches = new ArrayList<String>();
        while ((line = reader.readLine()) != null)
        {
            teams.add(line);
        }
        switch (round)
        {
            case 1:
                matches.add(teams.get(0)); matches.add(teams.get(1));
                matches.add(teams.get(2)); matches.add(teams.get(3));
                break;
            case 2:
                matches.add(teams.get(0)); matches.add(teams.get(3));
                matches.add(teams.get(1)); matches.add(teams.get(2));
                break;
            case 3:
                matches.add(teams.get(0)); matches.add(teams.get(2));
                matches.add(teams.get(1)); matches.add(teams.get(3));
                break;
        }
        return matches;
    }
    
}
